package com.qbw.app.model;

import androidx.databinding.Bindable;

import com.google.gson.annotations.SerializedName;
import com.qbw.app.BR;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author QBW
 * @date 2019/8/29
 */
public class Vehicle extends BaseModel implements Serializable {

    /**
     * 1	电瓶电压	BatteryVoltage
     * 2	冷却液温度	CoolantTemperature
     * 3	车辆故障	VehicleBreakDown
     * 4	预估里程	EstimatedMileage
     * 5	熄火后震动	ShockFlameout
     * 6	疑似碰撞	SuspectedCollision
     * 7	疑似翻车	SuspectedRollover
     * 8	设备拔出	DevicePullOut
     * 9	非法位移	IllegalDisplacement
     * 10	急加速	RapidAcceleration
     * 11	急减速	RapidDeceleration
     * 12	急转弯	RapidSharpTurn
     */
    public static final int MENU_ID_BATTERYVOLTAGE = 1;
    public static final int MENU_ID_COOLANTTEMPERATURE = 2;
    public static final int MENU_ID_VEHICLEBREAKDOWN = 3;
    public static final int MENU_ID_ESTIMATEDMILEAGE = 4;
    public static final int MENU_ID_SHOCKFLAMEOUT = 5;
    public static final int MENU_ID_SUSPECTEDCOLLISION = 6;
    public static final int MENU_ID_SUSPECTEDROLLOVER = 7;
    public static final int MENU_ID_DEVICEPULLOUT = 8;
    public static final int MENU_ID_ILLEGALDISPLACEMENT = 9;
    public static final int MENU_ID_RAPIDACCELERATION = 10;
    public static final int MENU_ID_RAPIDDECELERATION = 11;
    public static final int MENU_ID_RAPIDSHARPTURN = 12;
    public static final int MENU_ID_ADSORBENT = 15;
    public static final int MENU_ID_TIRE = 16;
    /**
     * 剩余油量
     */
    public static final int MENU_ID_OIL_REMAINDER = 14;
    public static final int LIGHT_ON = 1;

    /**
     * 能源类型 1:汽油 2:柴油 3:压缩天然气（GNC）4:液化天然气（LNC）5:油电混合动力 6:电力
     */
    private int fuelType;
    private boolean index;
    private String vehicleCode;
    private String vehicleNick;
    private String seriesName;
    private String vehicleNumber;
    @SerializedName("brandName")
    private String vehicleBrand;
    @SerializedName("year")
    private String vehicleYear;
    @SerializedName("vin")
    private String vehicleVin;
    /**
     * 车辆型号
     */
    @SerializedName("modelName")
    private String vehicleType;
    @SerializedName("engineNo")
    private String engineNumber;
    private boolean selected = true;
    private int drivingState;
    private List<Integer> menuIds;

    private String aesKey;
    private String tboxKey;
    private String mac;
    /**
     * 一键寻车等待时间
     */
    private int waitTime;
    /**
     * 1,支持大灯；0不支持
     */
    private int light;

    // 诺马
    private String deviceCode;
    private String devicePassword;

    public Vehicle() {
    }

    public Vehicle(String vehicleCode) {
        this.vehicleCode = vehicleCode;
    }

    public Vehicle(String vehicleNick, String vehicleVin) {
        this.vehicleNick = vehicleNick;
        this.vehicleVin = vehicleVin;
    }


    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }

    public String getTboxKey() {
        return tboxKey;
    }

    public void setTboxKey(String tboxKey) {
        this.tboxKey = tboxKey;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public int getFuelType() {
        return fuelType;
    }

    public void setFuelType(int fuelType) {
        this.fuelType = fuelType;
    }

    public boolean isIndex() {
        return index;
    }

    public void setIndex(boolean index) {
        this.index = index;
    }

    public String getVehicleCode() {
        return vehicleCode;
    }

    public void setVehicleCode(String vehicleCode) {
        this.vehicleCode = vehicleCode;
    }

    @Bindable
    public String getVehicleNick() {
        return vehicleNick;
    }

    public void setVehicleNick(String vehicleNick) {
        this.vehicleNick = vehicleNick;
        notifyPropertyChanged(BR.vehicleNick);
    }

    @Bindable
    public String getVehicleNumber() {
        return vehicleNumber;
    }

    public void setVehicleNumber(String vehicleNumber) {
        this.vehicleNumber = vehicleNumber;
        notifyPropertyChanged(BR.vehicleNumber);
    }


    @Bindable
    public String getSeriesName() {
        return seriesName;
    }

    public void setSeriesName(String seriesName) {
        this.seriesName = seriesName;
    }

    @Bindable
    public String getVehicleBrand() {
        return vehicleBrand;
    }

    @Bindable
    public String getVehicleYear() {
        return vehicleYear;
    }

    @Bindable
    public String getVehicleVin() {
        return vehicleVin;
    }

    public void setVehicleBrand(String vehicleBrand) {
        this.vehicleBrand = vehicleBrand;
        notifyPropertyChanged(BR.vehicleBrand);
    }

    public void setVehicleYear(String vehicleYear) {
        this.vehicleYear = vehicleYear;
        notifyPropertyChanged(BR.vehicleYear);
    }

    public void setVehicleVin(String vehicleVin) {
        this.vehicleVin = vehicleVin;
        notifyPropertyChanged(BR.vehicleVin);
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
        notifyPropertyChanged(BR.vehicleType);
    }

    @Bindable
    public String getVehicleType() {
        return vehicleType;
    }

    @Bindable
    public String getEngineNumber() {
        return engineNumber;
    }

    public void setEngineNumber(String engineNumber) {
        this.engineNumber = engineNumber;
        notifyPropertyChanged(BR.engineNumber);
    }

    @Bindable
    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        notifyPropertyChanged(BR.selected);
    }

    @Bindable
    public int getDrivingState() {
        return drivingState;
    }

    public void setDrivingState(int drivingState) {
        this.drivingState = drivingState;
        notifyPropertyChanged(BR.drivingState);
    }

    public List<Integer> getMenuIds() {
        return menuIds;
    }

    public int getMenuIdSize() {
        return menuIds == null ? 0 : menuIds.size();
    }

    public boolean hasMenuId(int mid) {
        if (getMenuIdSize() <= 0) {
            return false;
        }
        for (int id : menuIds) {
            if (id == mid) {
                return true;
            }
        }
        return false;
    }


    public int getWaitTime() {
        return waitTime;
    }

    public void setWaitTime(int waitTime) {
        this.waitTime = waitTime;
    }

    public int getLight() {
        return light;
    }

    public String getDeviceCode() {
        return deviceCode;
    }

    public void setDeviceCode(String deviceCode) {
        this.deviceCode = deviceCode;
    }

    public String getDevicePassword() {
        return devicePassword;
    }

    public void setDevicePassword(String devicePassword) {
        this.devicePassword = devicePassword;
    }

    public static class VehicleList extends BaseModelList<Vehicle> implements Serializable {

        /*@Deprecated
        public Vehicle getSelectedVehicle() {
            return getIndexVehicle();
        }

        @Deprecated
        public String getSelectedVehicleCode() {
            return getIndexVehicleCode();
        }*/

        public Vehicle getIndexVehicle() {
            int size = size();
            if (size <= 0) {
                return null;
            }
            for (Vehicle vehicle : getList()) {
                if (vehicle.isIndex()) {
                    return vehicle;
                }
            }
            return null;
        }

        public String getIndexVehicleCode() {
            Vehicle vehicle = getIndexVehicle();
            return vehicle == null ? "" : vehicle.getVehicleCode();
        }

        public Vehicle.VehicleList getSelectedVehicleList() {
            VehicleList vehicleList = new VehicleList();
            vehicleList.setList(new ArrayList<>());
            int size = size();
            if (size <= 0) {
                return vehicleList;
            }
            for (Vehicle vehicle : getList()) {
                if (vehicle.isSelected()) {
                    vehicleList.getList().add(vehicle);
                }
            }
            return vehicleList;
        }

        public String getVehicleNames() {
            StringBuilder sbNames = new StringBuilder();
            int size = size();
            if (size <= 0) {
                return null;
            }
            for (Vehicle vehicle : getList()) {
                sbNames.append(vehicle.getVehicleNick()).append(",");
            }
            if (sbNames.length() > 0) {
                sbNames.deleteCharAt(sbNames.length() - 1);
            }
            return sbNames.toString();
        }

        public String getSelectedVehicleNames() {
            StringBuilder sbNames = new StringBuilder();
            VehicleList svl = getSelectedVehicleList();
            for (Vehicle vehicle : svl.getList()) {
                sbNames.append(vehicle.getVehicleNick()).append(",");
            }
            if (sbNames.length() > 0) {
                sbNames.deleteCharAt(sbNames.length() - 1);
            }
            return sbNames.toString();
        }

        public List<String> getSelectedVehicleCodes() {
            List<String> svcs = new ArrayList<>();
            VehicleList svl = getSelectedVehicleList();
            for (Vehicle vehicle : svl.getList()) {
                svcs.add(vehicle.getVehicleCode());
            }
            return svcs;
        }

        public boolean isAllVehicleSelected() {
            return getSelectedVehicleList().size() == size();
        }
    }

    public static class Response extends BaseResponse<VehicleList> {}

    public static class ResponseOne extends BaseResponse<Vehicle> {}

}
